#pragma once
#include <sys/epoll.h>
#include <cstring>
#include "Log.hpp"
#include "Comm.hpp"
class Epoller
{
    const static int gsize = 128;
    public:
        Epoller()
        {
            _epfd = epoll_create(gsize);
            if(_epfd < 0)
            {
                LOG(FATAL,"epoll create error!\n");
                exit(EPOLL_CREATE_ERROR);
            }
            LOG(INFO,"epoll create success epfd:%d\n",_epfd);
        }
        bool AddEvent(int fd , uint32_t event)
        {
            struct epoll_event ep;

            ep.data.fd = fd;

            ep.events = event;
            int n = epoll_ctl(_epfd,EPOLL_CTL_ADD,fd,&ep);
            if(n < 0)
            {
                LOG(ERROR,"epoll add %d error!\n %s",fd,strerror(errno));
                return false;
            }
            LOG(INFO,"epoll add %d success!\n",fd);

            return true;
        }
        int Wait(epoll_event* events,int timeout,int num)
        {
            int n = epoll_wait(_epfd,events,num,timeout);
            if(n < 0)
            {
                LOG(ERROR,"epoll wait error!\n");
                return -1;
            }
            LOG(INFO,"epoll wait success! , n = %d\n",n);
            return n;
        }
    private:
        int _epfd;
};